package hn.sigit.logic.security;

import hn.sigit.logic.ladmshadow.Util;

import hn.sigit.model.hnd.administrative.HND_NaturalPerson;
import hn.sigit.dao.hnd.administrative.HND_NaturalPersonDAO;
import hn.sigit.model.ladm.administrative.LA_BAUnit;
import hn.sigit.model.ladm.administrative.LA_RightType;
import hn.sigit.model.ladm.external.ExtParty;
import hn.sigit.dao.ladm.external.ExtPartyDAO;
import hn.sigit.model.ladm.party.LA_Party;
import hn.sigit.dao.ladm.party.LA_PartyDAO;
import hn.sigit.model.ladm.spatialunit.LA_VolumeType;
import hn.sigit.model.ladm.spatialunit.LA_VolumeValue;
import hn.sigit.model.ladm.special.Rational;
import hn.sigit.recyclebin.TransactionController;
import hn.sigit.recyclebin.TransactionStatesXMLProducer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.Stack;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernatespatial.criterion.SpatialRestrictions;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.remoting.WebRemote;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import org.geotools.feature.FeatureCollections;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.WKTWriter;

@Name("testDao")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class TestDAO implements Serializable {
	private static final long serialVersionUID = 1L;

	@In
	private Session commonsDatabase;
	
	@In
	private TransactionController transactionController;
	
	public void testTransactionGML() {
		TransactionStatesXMLProducer xmlProducer = new TransactionStatesXMLProducer();

		ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
		xmlProducer.produceXml(baos);
		
		System.out.println( baos.toString() );
		
		
	}
	
	@WebRemote
	public String webRemote(double x, double y) {
		// TODO Auto-generated method stub
		System.out.println(String.format("Las coords son (%f, %f)", x, y));
		return String.valueOf(x + y);
	}

	private static SimpleFeatureType createFeatureType() {
		SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
		builder.setName("Location");
		builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference
													// system

		// add attributes in order
		builder.add("gid", Integer.class);
		builder.length(15).add("countryName", String.class); // <- 15 chars
																// width for
																// name field
		builder.add("theGeom", MultiPolygon.class);

		// build the type
		final SimpleFeatureType COUNTRY = builder.buildFeatureType();

		return COUNTRY;
	}
	
	public void testSavingMultiple() {
		HND_NaturalPerson natPersExtParty = new HND_NaturalPerson();
		System.out.println("ID: " + natPersExtParty.getExtPID());
		
		natPersExtParty.setFirstName1("Pancracio");
		natPersExtParty.setLastName1("LOOP");
		natPersExtParty.setGender('M');
		natPersExtParty.setMaritalStatus('S');
		
		ExtPartyDAO.save(natPersExtParty);
		System.out.println("Despues de 1ra salvada. ID: " + natPersExtParty.getExtPID());

		natPersExtParty = natPersExtParty.clone();
		natPersExtParty.setExtPID(0);
		natPersExtParty.setLastName1("LOOP_ZERO");
		
		ExtPartyDAO.save(natPersExtParty);
		System.out.println("Despues de 2da SALVADA. ID: " + natPersExtParty.getExtPID());

	}
	
	public void testCopyShadow() {
		//Util.copyPropertyData(2, 388, false);
		
	}
	
	public void testQuery() {
		List<LA_Party> list;
		list = HND_NaturalPersonDAO.loadParties();
		for (LA_Party lap : list)
			System.out.println(lap.getORMID());
	}

	private void writeZipFile(final File[] inputFiles, OutputStream outStream) {
		final int BUFFER = 4192;

		try {
			ZipOutputStream out = new ZipOutputStream(outStream);
			BufferedInputStream origin = null;
			byte data[] = new byte[BUFFER];
			for (File file : inputFiles) {
				if (file.getName().endsWith(".zip")) continue;
				
				try {
					FileInputStream fi = new FileInputStream(file);
					origin = new BufferedInputStream(fi, BUFFER);
					ZipEntry entry = new ZipEntry(file.getName());
					out.putNextEntry(entry);

					int count;
					while ((count = origin.read(data, 0, BUFFER)) != -1)
						out.write(data, 0, count);
					origin.close();
					System.out.println("Se zipeo " + file.getName());
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void dummy() {
		
	}
}
